package runtime;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * @author hubert
 * @date 2019/1/30
 * @description StreamReader
 */
public class StreamReader extends Thread{
    private Logger log = LoggerFactory.getLogger(this.getClass());

    private InputStream inputStream;

    public StreamReader(InputStream inputStream) throws IOException {
        this.inputStream = inputStream;
        super.start();
    }

    @Override
    public void run() {
        int counter ;
        try {
          /*  while ((counter = inputStream.read())!= -1) {
                if (counter != -1){}
            }*/
            BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
            String line;
            while ((line = br.readLine()) != null) {
                //stringBuilder.append(line);
                if (!"".equals(line)){
                    log.info("result:"+line);
                }
            }
            br.close();
        } catch (Exception e) {
            log.error("catch:"+e.getMessage(),e);
            Thread.currentThread().interrupt();
        } finally {
            try {
                inputStream.close();
            } catch (Exception e) {
                log.error("finally_catch:"+e.getMessage(),e);
                Thread.currentThread().interrupt();
            }
        }
    }
}
